package com.xxxx.yeb.utils;

import com.xxxx.yeb.pojo.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 2 * @Author: yangxin
 * 3 * @Date: 2020/7/22 16:21
 * 4
 */
public class PoiUtils {

    /**
     * 解析文件
     *
     * @param file
     * @param allNations
     * @param allPolitics
     * @param allDeps
     * @param allPos
     * @param allJobLevels
     * @return
     */
    public static List<Employee> importEmployeeByExcel(MultipartFile file, List<Nation> allNations,
                                                       List<PoliticsStatus> allPolitics,
                                                       List<Department> allDeps,
                                                       List<Position> allPos,
                                                       List<Joblevel> allJobLevels) {
        //定义员工POJO类
        Employee emp;
        //解析出来的封装好要返回的对象
        List<Employee> emps = new ArrayList<>();
        //插入数据库时，需要日期格式转换
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        //先判断客户端上传的excel有没有效
        if (file.isEmpty()) {
            throw new RuntimeException("文件错误");
        }
        try {
            //获得上传的excel文件
            HSSFWorkbook workbook =
                    new HSSFWorkbook(new POIFSFileSystem(file.getInputStream()));
            //获取第一个sheet
            HSSFSheet sheet = workbook.getSheetAt(0);
            //获取行数
            int rowNums = sheet.getPhysicalNumberOfRows();
            //遍历行数
            for (int i = 1; i < rowNums; i++) {
                emp = new Employee();
                //得到该行的数据
                HSSFRow row = sheet.getRow(i);
                //获得该行的单元格数量，并遍历出单元格
                int cellNums = row.getPhysicalNumberOfCells();
                for (int j = 0; j < cellNums; j++) {
                    HSSFCell cell = row.getCell(j);
                    if (cell != null) {
                        //开始填装Emp实体属性
                        String value = cell.getStringCellValue();
                        if (value == null) {
                            //如果值是空的则空字符串
                            value = "";
                        }

//每一个字段对应的index
                        switch (j) {
                            case 0:
                                emp.setName(value);
                                break;
                            case 1:
                                emp.setWorkID(value);
                                break;
                            case 2:
                                emp.setGender(value);
                                break;
                            case 3:
                                emp.setBirthday(LocalDate.parse(value, format));
                                break;
                            case 4:
                                emp.setIdCard(value);
                                break;
                            case 5:
                                emp.setWedlock(value);
                                break;
                            case 6:
                                // 民族需要转换成id，目前是name

                                for (int k = 0; k < allNations.size(); k++) {
                                    if (allNations.get(k).getName().equals(value)) {
                                        emp.setNationId(allNations.get(k).getId());
                                        break;
                                    }
                                }
                                break;
                            case 7:
                                emp.setNativePlace(value);
                                break;
                            case 8:
                                //政治面貌需要转换成Id
                                for ( int k = 0; k < allPolitics.size(); k++) {
                                    if (allPolitics.get(k).getName().equals(value)) {
                                        emp.setPoliticId(allPolitics.get(k).getId());
                                        break;
                                    }
                                }
                                break;
                            case 9:
                                //电子邮件
                                emp.setEmail(value);
                                break;
                            case 10:
                                //电话号码
                                emp.setPhone(value);
                                break;
                            case 11:
                                //联系地址
                                emp.setAddress(value);
                                break;
                            case 12:
                                //所属部门 --需要转换成Id
                                for (int k = 0; k < allDeps.size(); k++) {
                                    if (allDeps.get(k).getName().equals(value)) {
                                        emp.setDepartmentId(allDeps.get(k).getId());
                                        break;
                                    }
                                }
                                break;
                            case 13:
                                //职称 -- 需要转换成ID
                                for (int k = 0; k < allJobLevels.size(); k++) {
                                    if (allJobLevels.get(k).getName().equals(value)) {
                                        emp.setJobLevelId(allJobLevels.get(k).getId());
                                        break;
                                    }
                                }
                                break;
                            case 14:
                                //职位 -- 需要转换成id
                                for (int k = 0; k < allPos.size(); k++) {
                                    if (allPos.get(k).getName().equals(value)) {
                                        emp.setPosId(allPos.get(k).getId());
                                        break;
                                    }
                                }
                                break;
                            case 15:
                                //聘用形式
                                emp.setEngageForm(value);
                                break;
                            case 16:
                                //最高学历
                                emp.setTiptopDegree(value);

                                break;
                            case 17:
//                                毕业院校
                                emp.setSchool(value);

                                break;
                            case 18:
//                                专业
                                emp.setSpecialty(value);

                                break;
                            case 19:
//                                在职状态
                                emp.setWorkState(value);

                                break;
                            case 20:
                                //入职日期
                                emp.setBeginDate(LocalDate.parse(value, format));

                                break;
                            case 21:
                                //转正日期
                                emp.setConversionTime(LocalDate.parse(value, format));
                                break;
                            case 22:
                                //合同起始日期
                                emp.setBeginContract(LocalDate.parse(value, format));
                                break;
                            case 23:
                                //合同截止日期
                                emp.setEndContract(LocalDate.parse(value, format));
                                break;
                            case 24:
                                //合同期限
                                emp.setContractTerm(Double.parseDouble(value));
                                break;
                            default:
                                break;
                        }
                    }

                }
                //一个完整emp
                // 将添加好的emp添加到list
                emps.add(emp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return emps;
    }
}
